package com.fudian.spark_platform

import java.util.Base64

import com.fudian.spark_platform.Configure._
import net.minidev.json.JSONObject
import net.minidev.json.parser.JSONParser


/**
  * CopyRrigt FuMan AI DataDimming.co SHANGHAI,RPC
  * File can't be used to License
  * Created by XiaoJun on 2018/3/6.
  */
object index {

    var appSettingConf:AppSettingConfig = new AppSettingConfig()
    var dataSourceConf:DataSourceInputConfig = new DataSourceInputConfig()
    var millConf:MillConfig = new MillConfig()
    var transformConf:TransformConfig = new TransformConfig()
    var mlConf:MLConfig = new MLConfig()
    var outputConf:OutputConfig = new OutputConfig()

    def main(args: Array[String]) {

        //首先获取配置参数
        if(args.length > 0){
            settingsConf(args(0))
        }else{
            settingsConf("eyJBcHBTZXR0aW5nQ29uZiI6eyJBcHBOYW1lIjoieGlhb2p1biJ9fQ==")
        }


    }


    def settingsConf(conf: String) {
        //base64解码
        val jsonStr = new String(Base64.getDecoder.decode(conf))
        //接受到的参数一个json字符串
        val jsonParser = new JSONParser()
        val jsonConf:JSONObject = jsonParser.parse(jsonStr).asInstanceOf[JSONObject]
        setConf(jsonConf)

    }


    /**
      * 配置公有的数据库等配置信息
      *
      * @param conf 配置Map
      */
    def setConf(conf:JSONObject) = {


        val jsonKey = conf.keySet()
        val iter = jsonKey.iterator()

        while (iter.hasNext){
            val instance = iter.next()
            val value = conf.get(instance)
            value match {
                case appConf: JSONObject =>
                    instance match {
                        case "AppSettingConf" => {
                            this.appSettingConf.setConf(appConf)
                        }
                    }
                case _ =>
            }
            println(this.appSettingConf.settings)
            println("key: " + instance + "value: " + value.toString)
        }



//        try{
//            //首先设置spark运行基本参数
//            val g_AppSettingConf = conf.getJSONObject("AppSettingConfig")
//            this.appSettingConf = this.appSettingConf.setConf(conf.getJSONObject("AppSettingConfig"))
//            println("AppSettingConf:" + this.appSettingConf.settings.toString)
//        } catch {
//            case e:Exception => {
//                print("执行配置覆盖失败")
//                this.appSettingConf = new AppSettingConfig()
//            }
//        }
//
//        //再设置数据源配置
//        this.dataSourceConf = this.dataSourceConf.setConf(conf.getJSONObject("DataSourceInputConfig"))
//        println("DataSourceInputConf:" + this.dataSourceConf.inputConfig.toString)
//
//        //再设置数据清洗配置
//        this.millConf = this.millConf.setConf(conf.getJSONObject("MillConfig"))
//        println("MillConf:" + this.millConf.millingConfig.toString)
//
//        //再设置数据转化配置
//        this.transformConf = this.transformConf.setConf(conf.getJSONObject("TransformConfig"))
//        println("TransformConf:" + this.transformConf.transformConfig.toString)
//
//        //再设置数据算法配置
//        this.mlConf = this.mlConf.setConf(conf.getJSONObject("MLConfig"))
//        println("MlConf:" + this.mlConf.mLConfig.toString)
//
//        //再设置数据输出配置
//        this.outputConf = this.outputConf.setConf(conf.getJSONObject("OutputConfig"))
//        println("OutputConf:" + this.outputConf.outConfig.toString)


    }

}
